<?php

    $page_title = "New | Box";
    include 'includes/header.php';
    include 'includes/common.php';

    $q_check_get = "SELECT MAX(new_id) as mx FROM news";
    $result1 = mysqli_query($db_connect, $q_check_get);
    $author_count = mysqli_fetch_array($result1, MYSQLI_ASSOC);
    $mx = $author_count['mx'];

    if(array_key_exists('new', $_GET) && (int)$_GET['new'] <= $mx && (int)$_GET['new'] > 0) {
        $new_id = $_GET['new'];

        $q = "SELECT new_id, new_title, new_text, new_pic_loc FROM news WHERE new_id = '$new_id'";

        $result = mysqli_query($db_connect, $q);

        $selected_new = array();

        while($row = mysqli_fetch_assoc($result)) {
            $selected_new['new_id'] = $row['new_id'];
            $selected_new['new_title'] = $row['new_title'];
            $selected_new['new_text'] = $row['new_text'];
            $selected_new['new_pic_loc'] = $row['new_pic_loc'];
        }

        if(isset($selected_new['new_id'])) {

?>

    <!-- FACEBOOK COMMENTS DIV - DO NOT REMOVE -->
    <div id="fb-root"></div>
    <script>(function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));</script>


	<!-- web content -->
	<div id="container_content">
		<div id="read_news_container">
			<h2><?php echo $selected_new['new_title'];?></h2>
			<div id="novina">
				<div id="news_pic">
					<img src="<?php echo $selected_new['new_pic_loc'];?>" alt="">
				</div>
				<div id="news_text">
					<p><?php echo nl2br($selected_new['new_text']);?></p>
				</div>
			</div>
		</div>
		<div id="comments_container">
            <div class="fb-comments" data-href="#"
            data-colorscheme="dark" data-numposts="10" data-width="900"></div>
		</div>
	</div>
<?php
        }
    } else {
        echo "Invalid new selected. <a href='index.php'>Home</a>";
    }

    include 'includes/footer.php';

?>